package application.exportimport;

import domain.cms.AbstractPage;
import domain.cms.CmsPage;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * Wrapper for content inventory
 */
public final class WrappedContent {

    private List<Page> pages = new LinkedList<Page>();

    /**
     * Add to wrappedContent entry CmsPage, its CmsCategories and its CmsPages.
     *
     * @param cmsPage
     */
    public void addPage(CmsPage cmsPage) {
        transformAndAddPage(cmsPage, true, false, null, null);
        List<CmsPage> categories = cmsPage.getChilds();
        List<CmsPage> cmsPages = null;
        for (CmsPage page : categories) {
            transformAndAddPage(cmsPage, false, true, page.getTitle(), null);

            cmsPages = cmsPage.getChilds();
            for (CmsPage foo : cmsPages) {
                transformAndAddPage(cmsPage, false, false, foo.getTitle(), foo.getTitle());
            }
        }
    }

    /**
     * Extract data from AbstractPage and entry fields.
     * Then create new Page and add it to collection pages.
     *
     * @param ap
     * @param isSection
     * @param isCategory
     * @param section    parent section of ap
     * @param category   parent category of ap
     */
    private void transformAndAddPage(AbstractPage ap, Boolean isSection, Boolean isCategory,
                                     String section, String category) {

        String title = ap.getTitle();
        String keywords = ap.getKeywords();
        String description = ap.getDescription();
        Date lastEditedDate = ap.getLastEditedDate();
        String creator = ap.getUser().getLogin();
        pages.add(new Page(title, keywords, description, lastEditedDate, creator,
                isSection, isCategory, section, category));
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("<table>\n");
        sb.append("  <thead>\n");
        sb.append(Page.head);
        sb.append("  </thead>\n");
        sb.append("  <tbody>\n");
        for (Page page : pages) {
            sb.append(page.toString());
        }
        sb.append("  </tbody>\n");
        sb.append("</table>\n");
        sb.append("<br />\n");
        return sb.toString();
    }

    /**
     * Wrapper class page contains only few crucial page parameters.
     */
    private final class Page {

        /**
         * head of the table
         */
        public static final String head = "    <tr>" +
                "<th>title</th><th>keywords</th><th>description</th><th>lastEditedDate</th>" +
                "<th>creator</th><th>isSection</th><th>isCategory</th><th>section</th><th>category</th>" +
                "</tr>\n";
        /**
         * cms attributes
         */
        private String title;
        private String keywords;
        private String description;
        private Date lastEditedDate;
        /**
         * other params
         */
        private String creator;
        private Boolean isSection;
        private Boolean isCategory;
        private String section;
        private String category;

        /**
         * Constructor of all fields.
         *
         * @param title          cms attribute
         * @param keywords       cms attribute
         * @param description    cms attribute
         * @param lastEditedDate
         * @param creator        the one, who created page (user in entity)
         * @param isSection
         * @param isCategory
         * @param section        parent section, if any (set null if not)
         * @param category       parent category, if any (set null if not)
         */
        public Page(String title, String keywords, String description, Date lastEditedDate,
                    String creator, Boolean isSection, Boolean isCategory, String section, String category) {
            this.title = title;
            this.keywords = keywords;
            this.description = description;
            this.lastEditedDate = lastEditedDate;
            this.creator = creator;
            this.isSection = isSection;
            this.isCategory = isCategory;
            this.section = section;
            this.category = category;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("    <tr>");

            sb.append("<td>" + title + "</td>");
            sb.append("<td>" + keywords + "</td>");
            sb.append("<td>" + description + "</td>");
            sb.append("<td>" + lastEditedDate.toString() + "</td>");
            sb.append("<td>" + creator + "</td>");
            sb.append("<td>" + isSection + "</td>");
            sb.append("<td>" + isCategory + "</td>");
            sb.append("<td>" + section + "</td>");
            sb.append("<td>" + category + "</td>");

            sb.append("</tr>\n");
            return sb.toString();
        }
    }
}
